package virtualadvisor;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.AbstractListModel;
import javax.swing.JScrollPane;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class MainGUI {

	JFrame frame_MainGui;
	private JTextField textField_TotalCredits_Semester;
	private JTextField textField_Major;
	private JTextField txtField_Minor;
	private static boolean mainGuiView;
	private JTree tree_CoursesNeeded;
	private JTree tree_CoursesTaken;
	private JTextPane textPanel_CourseDescription;
	private JList list_Schedule;
	private JComboBox comboBox;
	private JButton button_AddViewComments;
	private JButton button_RemoveCourse;
	private JButton button_ChangeMinor;
	private final JPanel contentPanel = new JPanel();
	private Profile mainProfile;
	private Student student;
	private Course currentCourseSelected;
	private Course takenCourseSelected;
	private DegreeProgress dp;
	private String file = "courseCatalog.xml";
	private String rootMajor = "Computer Science Major (CMSC)";
	static private final boolean DEBUG = false; 
	private ArrayList<Course> currentCourses;
	static private boolean first = false;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		LinkedList<String> major = new LinkedList<String>();
		major.add("Computer Science Major (CMSC)");
		Profile p = new Profile(new Student("Ally", major, null ));
		try {
			if(!DEBUG){
				File f = new File("Profile.xml");
				if(f.exists() == false){
					ProfileWizard dialog = new ProfileWizard(p);
					first = true;
					dialog.setResizable(false);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} else {
					ProfileSelector dialog = new ProfileSelector(p);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				}
			} else {
				MainGUI window = new MainGUI(p);
				window.frame_MainGui.setVisible(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the application.
	 * @wbp.parser.entryPoint
	 */
	public MainGUI(Profile p) {
		mainProfile = p;
		dp = p.getProgress();
		student = p.getStudent();
		mainGuiView = false;
		initialize();
	}
	
	public void run() {
		try {
			if(!DEBUG){
				File f = new File("Profile.xml");
				if(f.exists() == false){
					ProfileWizard dialog = new ProfileWizard(mainProfile);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} else {
					ProfileSelector dialog = new ProfileSelector(mainProfile);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				}
			} else {
				MainGUI window = new MainGUI(mainProfile);
				window.frame_MainGui.setVisible(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Initialize the contents of the main frame.
	 */
	public void initialize() {
		frame_MainGui = new JFrame();
		frame_MainGui.setResizable(false);
		frame_MainGui.setTitle("Virtual Advisor");
		frame_MainGui.setBounds(100, 100, 617, 507);
		frame_MainGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame_MainGui.getContentPane().setLayout(null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 10, 589, 442);
		frame_MainGui.getContentPane().add(tabbedPane);
		
		JPanel tabPanel_Courses = new JPanel();
		tabbedPane.addTab("Courses", null, tabPanel_Courses, null);
		tabPanel_Courses.setLayout(null);
		
		JLabel label_CoursesNeeded = new JLabel("Courses Needed");
		label_CoursesNeeded.setBounds(10, 10, 103, 14);
		label_CoursesNeeded.setFont(new Font("Tahoma", Font.BOLD, 12));
		tabPanel_Courses.add(label_CoursesNeeded);
		
		JLabel label_CoursesTaken = new JLabel("Courses Taken");
		label_CoursesTaken.setBounds(10, 197, 103, 14);
		label_CoursesTaken.setFont(new Font("Tahoma", Font.BOLD, 12));
		tabPanel_Courses.add(label_CoursesTaken);
		
		JLabel label_CourseDescription = new JLabel("Course Description");
		label_CourseDescription.setBounds(254, 10, 130, 14);
		label_CourseDescription.setFont(new Font("Tahoma", Font.BOLD, 12));
		tabPanel_Courses.add(label_CourseDescription);
		
		JButton button_AddCourseToSchedule = new JButton("Add Course To Schedule");
		button_AddCourseToSchedule.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if(currentCourseSelected == null){
						JOptionPane.showMessageDialog(contentPanel,
								"Please Select a Course from \nthe Courses Needed Section.",
								"Warning", JOptionPane.WARNING_MESSAGE);
					} else {
						TermSelector dialog = new TermSelector();
						dialog.setVisible(true);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	
		button_AddCourseToSchedule.setBounds(385, 375, 189, 23);
		button_AddCourseToSchedule.setFont(new Font("Tahoma", Font.PLAIN, 12));
		tabPanel_Courses.add(button_AddCourseToSchedule);
		
		JScrollPane scrollPanel_CoursesNeeded = new JScrollPane();
		scrollPanel_CoursesNeeded.setBounds(10, 30, 228, 152);
		tabPanel_Courses.add(scrollPanel_CoursesNeeded);

		
///////////////////////////////////		
		tree_CoursesNeeded = new JTree();
		DefaultMutableTreeNode top =new DefaultMutableTreeNode(rootMajor);
	    createNodes(top, 'n');
	    tree_CoursesNeeded = new JTree(top);
	    tree_CoursesNeeded.addTreeSelectionListener(new TreeSelectionListener(){
	    	public void valueChanged(TreeSelectionEvent e) {
	    		DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree_CoursesNeeded.getLastSelectedPathComponent();

	    		if (node == null) 
	    			return;

	    		Object nodeInfo = node.getUserObject();
	    		if (node.isLeaf()) {
	    			currentCourseSelected = mainProfile.findCourseInfoXML(file, nodeInfo.toString());
	    			
	    			if(currentCourseSelected.getName().equals(nodeInfo))
	    				textPanel_CourseDescription.setText(currentCourseSelected.descToString());

	    			//removeNode(node);
	    		} else {
	    			textPanel_CourseDescription.setText("");
	    		}
	    	}
	    });
				
		scrollPanel_CoursesNeeded.setViewportView(tree_CoursesNeeded);
		
		JScrollPane scrollPanel_CoursesTaken = new JScrollPane();
		scrollPanel_CoursesTaken.setBounds(10, 217, 228, 152);
		tabPanel_Courses.add(scrollPanel_CoursesTaken);
		
		
//////////////////////////////	
		tree_CoursesTaken = new JTree();
		DefaultMutableTreeNode top2 =new DefaultMutableTreeNode(rootMajor);
	    createNodes(top2, 't');
	    tree_CoursesTaken = new JTree(top2);
	    tree_CoursesTaken.addTreeSelectionListener(new TreeSelectionListener(){
	    	public void valueChanged(TreeSelectionEvent e) {
	    		DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree_CoursesTaken.getLastSelectedPathComponent();

	    		if (node == null) 
	    			return;

	    		Object nodeInfo = node.getUserObject();
	    		if (node.isLeaf()) {
	    			Course c = mainProfile.findCourseInfoXML(file, nodeInfo.toString());
	    			currentCourseSelected = c;
	    			takenCourseSelected = c;
	    			if(c.getName().equals(nodeInfo))
	    				textPanel_CourseDescription.setText(c.descToString());
	    			
	    			//removeNode(node);
	    		} 	
	    	}
	    });
		scrollPanel_CoursesTaken.setViewportView(tree_CoursesTaken);
//////////////////////////////		
		
	
		JScrollPane scrollPanel_CourseDescription = new JScrollPane();
		scrollPanel_CourseDescription.setBounds(254, 30, 320, 339);
		tabPanel_Courses.add(scrollPanel_CourseDescription);
		
		textPanel_CourseDescription = new JTextPane();
		scrollPanel_CourseDescription.setViewportView(textPanel_CourseDescription);
		textPanel_CourseDescription.setEditable(false);
		
		JButton button_AddGrade = new JButton("Add Grade");
		button_AddGrade.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(takenCourseSelected != null){
					AddGrade dialog = new AddGrade(takenCourseSelected);
					dialog.setVisible(true);
				} else {
					JOptionPane.showMessageDialog(contentPanel,
							"Please Select a Course from the \nCourses Taken Section.",
							"Warning", JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		button_AddGrade.setFont(new Font("Tahoma", Font.PLAIN, 12));
		button_AddGrade.setBounds(271, 375, 108, 23);
		tabPanel_Courses.add(button_AddGrade);
		
		JPanel tabPanel_Schedule = new JPanel();
		tabbedPane.addTab("Schedule", null, tabPanel_Schedule, null);
		tabPanel_Schedule.setLayout(null);
		
		JLabel label_Term = new JLabel("Term -");
		label_Term.setBounds(10, 10, 46, 14);
		label_Term.setFont(new Font("Tahoma", Font.BOLD, 12));
		tabPanel_Schedule.add(label_Term);
		
		JLabel label_CourseNumber = new JLabel("Course Number");
		label_CourseNumber.setBounds(10, 38, 93, 14);
		label_CourseNumber.setFont(new Font("Tahoma", Font.BOLD, 12));
		tabPanel_Schedule.add(label_CourseNumber);
		
		JLabel label_CourseName = new JLabel("Course Name");
		label_CourseName.setBounds(138, 38, 84, 14);
		label_CourseName.setFont(new Font("Tahoma", Font.BOLD, 12));
		tabPanel_Schedule.add(label_CourseName);
		
		JLabel label_Credits = new JLabel("Credits");
		label_Credits.setBounds(503, 38, 46, 14);
		label_Credits.setFont(new Font("Tahoma", Font.BOLD, 12));
		tabPanel_Schedule.add(label_Credits);
		
		JLabel label_TotalCredits = new JLabel("Total Credits");
		label_TotalCredits.setBounds(410, 200, 93, 14);
		label_TotalCredits.setFont(new Font("Tahoma", Font.BOLD, 12));
		tabPanel_Schedule.add(label_TotalCredits);
		
		textField_TotalCredits_Semester = new JTextField();
		textField_TotalCredits_Semester.setText("   4");
		textField_TotalCredits_Semester.setBounds(503, 197, 46, 20);
		textField_TotalCredits_Semester.setEditable(false);
		tabPanel_Schedule.add(textField_TotalCredits_Semester);
		textField_TotalCredits_Semester.setColumns(10);
		
		button_RemoveCourse = new JButton("Remove Course");
		button_RemoveCourse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if(list_Schedule.getSelectedValue() == null){
						JOptionPane.showMessageDialog(contentPanel,
								"Please Select a Course to Remove.",
								"Warning", JOptionPane.WARNING_MESSAGE);
					} else {
						int warningYesNo =JOptionPane.showConfirmDialog(null, 
								"Are you sure you want to remove \ncourse from your schedule?", 
								"Virtual Advisor", JOptionPane.YES_NO_OPTION);
						if (warningYesNo == JOptionPane.YES_OPTION) {
							dp = mainProfile.getProgress();
							String s = list_Schedule.getSelectedValue().toString();
							String[] ss = s.split(" ");
							Plan p = dp.getPlan(mainProfile.getCurrentPlanName(), ss[0]+ss[1]);
							String term = comboBox.getSelectedItem().toString();
							term = term.trim();
							String[] tterm = term.split(" ");
							Semester ts = p.getSemester(tterm[0], Integer.parseInt(tterm[1]));
							ts.deleteCourse(ss[0]+ss[1]);
							System.out.println("DELETE this course - " + list_Schedule.getSelectedValue());
							button_RemoveCourse.addActionListener(new RemoveCourseListner());
						} 
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		button_RemoveCourse.setBounds(429, 367, 133, 23);
		button_RemoveCourse.setFont(new Font("Tahoma", Font.PLAIN, 11));
		tabPanel_Schedule.add(button_RemoveCourse);
		
		button_AddViewComments = new JButton("Add / View Course Comments");
		button_AddViewComments.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if(list_Schedule.getSelectedValue() == null){
						JOptionPane.showMessageDialog(contentPanel,
								"Please Select a Course to Get Comments.",
								"Warning", JOptionPane.WARNING_MESSAGE);
					} else {
						CourseComments dialog = new CourseComments((Course)list_Schedule.getSelectedValue());
						dialog.setVisible(true);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		button_AddViewComments.setBounds(227, 367, 201, 23);
		button_AddViewComments.setFont(new Font("Tahoma", Font.PLAIN, 11));
		tabPanel_Schedule.add(button_AddViewComments);
		
		JScrollPane scrollPanel_Schedule = new JScrollPane();
		scrollPanel_Schedule.setBounds(10, 64, 539, 121);
		tabPanel_Schedule.add(scrollPanel_Schedule);
		
		
		comboBox = new JComboBox();
		list_Schedule = new JList();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				list_Schedule.setModel(new AbstractListModel() {
					private static final long serialVersionUID = 1L;
					ArrayList<Course> values = getCourse();
					
					public int getSize() {
						return values.size();
					}
					public Object getElementAt(int index) {
						return values.get(index);
					}
				});
			}
		});
		if(!first){
		comboBox.setModel(new DefaultComboBoxModel(mainProfile.getPlannedSemesters(mainProfile.getCurrentPlanName())));
		comboBox.setBounds(51, 4, 266, 27);
		tabPanel_Schedule.add(comboBox);
		
		list_Schedule = new JList();
		scrollPanel_Schedule.setViewportView(list_Schedule);
		list_Schedule.setModel(new AbstractListModel() {
			private static final long serialVersionUID = 1L;
			//String[] values = mainProfile.getPlannedSemesters(mainProfile.getCurrentPlanName());
			ArrayList<Course> values = getCourse();
			//TODO
			
			
			public int getSize() {
				return values.size();
			}
			public Object getElementAt(int index) {
				return values.get(index);
			}
		});
		}
		JPanel tabPanel_Profile = new JPanel();
		tabbedPane.addTab("Profile", null, tabPanel_Profile, null);
		tabPanel_Profile.setLayout(null);
		
		JLabel label_Major = new JLabel("Major -");
		label_Major.setFont(new Font("Tahoma", Font.BOLD, 11));
		label_Major.setBounds(10, 29, 46, 14);
		tabPanel_Profile.add(label_Major);
		
		JLabel label_Minor = new JLabel("Minor -");
		label_Minor.setFont(new Font("Tahoma", Font.BOLD, 11));
		label_Minor.setBounds(10, 65, 46, 14);
		tabPanel_Profile.add(label_Minor);
		
		JLabel label_CurrentGpa = new JLabel("Current GPA -");
		label_CurrentGpa.setFont(new Font("Tahoma", Font.BOLD, 11));
		label_CurrentGpa.setBounds(10, 105, 83, 14);
		tabPanel_Profile.add(label_CurrentGpa);
		
		textField_Major = new JTextField();
		student = mainProfile.getStudent();
		List<String> maj = student.getMajor();
		textField_Major.setText(maj.get(0));
		textField_Major.setEditable(false);
		textField_Major.setBounds(62, 23, 318, 20);
		tabPanel_Profile.add(textField_Major);
		textField_Major.setColumns(10);
		
		txtField_Minor = new JTextField();
		student = mainProfile.getStudent();
		txtField_Minor.setText(student.getMinor());
		txtField_Minor.setEditable(false);
		txtField_Minor.setBounds(62, 59, 318, 20);
		tabPanel_Profile.add(txtField_Minor);
		txtField_Minor.setColumns(10);
		
		JButton button_ChangeMajor = new JButton("Change Major");
		button_ChangeMajor.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent arg0) {
				try {
					MajorChange dialog = new MajorChange();
					dialog.setVisible(true);
					Student s = mainProfile.getStudent();
					textField_Major.setText(s.getMajor().get(0));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		button_ChangeMajor.setFont(new Font("Tahoma", Font.PLAIN, 11));
		button_ChangeMajor.setBounds(392, 23, 114, 23);
		tabPanel_Profile.add(button_ChangeMajor);
		
		button_ChangeMinor = new JButton("Change Minor");
		button_ChangeMinor.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent arg0) {
				try {
					MinorSelector dialog = new MinorSelector(mainProfile);
					dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		button_ChangeMinor.setFont(new Font("Tahoma", Font.PLAIN, 11));
		button_ChangeMinor.setBounds(392, 59, 114, 23);
		tabPanel_Profile.add(button_ChangeMinor);
		
		dp = mainProfile.getProgress();
		JLabel label_CurrentGPA = new JLabel(dp.getGPA().toString());
		label_CurrentGPA.setBounds(99, 105, 46, 14);
		tabPanel_Profile.add(label_CurrentGPA);
		
		JButton button_SaveProfile = new JButton("Save Profile");
		button_SaveProfile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mainProfile.saveProfileXML();
			}
		});
		button_SaveProfile.setBounds(472, 456, 117, 23);
		frame_MainGui.getContentPane().add(button_SaveProfile);
	}
	
	private ArrayList<Course> getCourse(){
		dp = mainProfile.getProgress();
		
		if(comboBox.getSelectedItem() != null){
			String s = (String)comboBox.getSelectedItem();
			s = s.trim();
			String[] ss = s.split(" ");
			currentCourses = dp.getCourseList(dp.getPlan(mainProfile.getCurrentPlanName(), ss[0]+ss[1]), ss[0], Integer.parseInt(ss[1]));
			textField_TotalCredits_Semester.setText("  " + getTotalCredits());
			return currentCourses;
		}
		return null;
	}
	
	private String getTotalCredits(){
		
		if(currentCourses != null){
			Integer cred = 0;
			for(int x = 0; x < currentCourses.size();x++){
				cred = cred + currentCourses.get(x).getCredits();
			}
			
			return cred.toString();
		}
		return "";
	}
	
 	public boolean getMainGuiView(){
		return mainGuiView;
	}
	
	public void updateMainGuiView(){
		if(mainGuiView == true){
			frame_MainGui.setVisible(false);
		} else {
			frame_MainGui.setVisible(true);
		}
	}
	
    private void createNodes(DefaultMutableTreeNode top, char flag) {
        DefaultMutableTreeNode dept = null;
        DefaultMutableTreeNode c = null;
        DegreeProgress d = mainProfile.getProgress();
        LinkedList<Course> lC = null;
        
        if(flag == 'n')
        	lC = d.getRemainingCourses();
        else if(flag == 't')
        	lC = d.getCompletedCourses();

        for(int x = 0; x < lC.size(); x++){
        	dept = new DefaultMutableTreeNode(lC.get(x).getDept());
        	DefaultMutableTreeNode temp = searchNode(top, lC.get(x).getDept());
        	
        	if(temp != null)
        		top.add(temp);
        	else {
        		top.add(dept);
        		for(int y = 0; y< lC.size(); y++)
        			if(lC.get(x).getDept().equals(lC.get(y).getDept())){
        				c = new DefaultMutableTreeNode(lC.get(y).getName());
        				dept.add(c);
        			}
        	}
        }
    }

    protected void removeNode(DefaultMutableTreeNode node, JTree tree) {
        DefaultTreeModel model = (DefaultTreeModel) (tree.getModel());
        TreePath[] paths = tree.getSelectionPaths();
        
        for (int i = 0; i < paths.length; i++) {
          node = (DefaultMutableTreeNode) (paths[i].getLastPathComponent());
          model.removeNodeFromParent(node);
        }
    }
    
    public DefaultMutableTreeNode searchNode(DefaultMutableTreeNode top, String nodeStr){ 
        DefaultMutableTreeNode node = null; 
         
        //Get the enumeration 
        @SuppressWarnings("rawtypes")
		Enumeration enu= top.breadthFirstEnumeration(); 
         
        //iterate through the enumeration 
        while(enu.hasMoreElements()) 
        { 
            //get the node 
            node = (DefaultMutableTreeNode)enu.nextElement(); 
             
            //match the string with the user-object of the node 
            if(nodeStr.equals(node.getUserObject().toString())) 
            { 
                //tree node with string found 
                return node;                          
            } 
        } 
         
        //tree node with string node found return null 
        return null; 
    }  

    class RemoveCourseListner implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
            int index = list_Schedule.getSelectedIndex();
            //listModel.remove(index);

            int size = 0;//listModel.getSize();

            if (size == 0) { //Nobody's left, disable firing.
            	button_RemoveCourse.setEnabled(false);
            	button_AddViewComments.setEnabled(false);
            } else { //Select an index.
                /*if (index == listModel.getSize()) {
                    //removed item in last position
                    index--;
                }*/

                list_Schedule.setSelectedIndex(index);
                list_Schedule.ensureIndexIsVisible(index);
            }
			
		}
    }
    
    class MinorSelector extends JDialog {

    	private static final long serialVersionUID = 1L;
    	private final JPanel contentPanel = new JPanel();
    	private Profile mainProfile;


    	/**
    	 * Create the dialog.
    	 */
    	public MinorSelector(Profile profile) {
    		mainProfile = profile;
    		setTitle("Virtual Advisor - Minor Selector");
    		setBounds(100, 100, 332, 115);
    		getContentPane().setLayout(new BorderLayout());
    		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    		getContentPane().add(contentPanel, BorderLayout.CENTER);
    		contentPanel.setLayout(null);
    		
    		JLabel label_SelectMinor = new JLabel("Select a Minor");
    		label_SelectMinor.setFont(new Font("Tahoma", Font.BOLD, 14));
    		label_SelectMinor.setBounds(10, 10, 149, 14);
    		contentPanel.add(label_SelectMinor);
    		String[] minorList = {"", "Computer Science Minor (CMSC)"};
    			
    		final JComboBox comboBox_SelectMinor = new JComboBox(minorList);
    		comboBox_SelectMinor.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    			}
    		});

    		comboBox_SelectMinor.setBounds(10, 30, 240, 22);
    		contentPanel.add(comboBox_SelectMinor);

    		JPanel buttonPane = new JPanel();
    		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		getContentPane().add(buttonPane, BorderLayout.SOUTH);
    		
    		JButton button_SaveMinor = new JButton("Save");
    		button_SaveMinor.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent arg0) {
    				String minorSelected = (String)comboBox_SelectMinor.getSelectedItem();
    				Student s = mainProfile.getStudent();
    				s.setMinor(minorSelected);
    				txtField_Minor.setText(student.getMinor());
    				dispose();
    			}
    		});
    			
    		button_SaveMinor.setActionCommand("OK");
    		buttonPane.add(button_SaveMinor);
    		getRootPane().setDefaultButton(button_SaveMinor);

    		JButton button_cancelSelectMinor = new JButton("Cancel");
    		button_cancelSelectMinor.addMouseListener(new MouseAdapter() {		
    			public void mouseClicked(MouseEvent arg0) {
    				dispose();
    			}
    		});
    		
    		button_cancelSelectMinor.setActionCommand("Cancel");
    		buttonPane.add(button_cancelSelectMinor);
    	}
    }

    class MajorChange extends JDialog {

    	private static final long serialVersionUID = 1L;
    	private final JPanel contentPanel = new JPanel();
    	private String[] majorList = {"", "Computer Science Major (CMSC)", "Information Systems Major (IS)"};

    	/**
    	 * Create the dialog.
    	 */
    	public MajorChange() {
    		setResizable(false);
    		setTitle("Virtual Advisor - Major Change");
    		setBounds(100, 100, 341, 190);
    		getContentPane().setLayout(new BorderLayout());
    		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    		getContentPane().add(contentPanel, BorderLayout.CENTER);
    		contentPanel.setLayout(null);

    		JLabel label_SelectMajor = new JLabel("Select a New Major");
    		label_SelectMajor.setFont(new Font("Tahoma", Font.BOLD, 14));
    		label_SelectMajor.setBounds(10, 10, 143, 22);
    		contentPanel.add(label_SelectMajor);
    			
    		final JComboBox comboBox_SelectMajor = new JComboBox(majorList);
    		comboBox_SelectMajor.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    			}
    		});
    			
    		comboBox_SelectMajor.setBounds(10, 30, 249, 22);
    		contentPanel.add(comboBox_SelectMajor);

    		JLabel label_SelectDoubleMajor = new JLabel("Select for a Double Major");
    		label_SelectDoubleMajor.setFont(new Font("Tahoma", Font.BOLD, 14));
    		label_SelectDoubleMajor.setBounds(10, 58, 209, 22);
    		contentPanel.add(label_SelectDoubleMajor);

    		final JComboBox comboBox_SelectDoubleMajor = new JComboBox(majorList);
    		comboBox_SelectDoubleMajor.setEnabled(false);
    		comboBox_SelectDoubleMajor.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    			}
    		});
    		
    		comboBox_SelectDoubleMajor.setBounds(10, 78, 249, 22);
    		contentPanel.add(comboBox_SelectDoubleMajor);

    		JLabel label_optional = new JLabel("(Optional)");
    		label_optional.setBounds(260, 86, 65, 14);
    		contentPanel.add(label_optional);

    		JPanel buttonPane = new JPanel();
    		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		getContentPane().add(buttonPane, BorderLayout.SOUTH);
    		JButton btnSave = new JButton("Save");
    		btnSave.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				String majorSelected = (String)comboBox_SelectMajor.getSelectedItem();
    				if(majorSelected.equals("")){
    					JOptionPane.showMessageDialog(contentPanel,
    						"Please Enter A New Major or click Cancel.",
    						"Warning", JOptionPane.WARNING_MESSAGE);
    				}else {
    					//String doubleMajorSelected = (String)comboBox_SelectDoubleMajor.getSelectedItem();
    					//System.out.println(majorSelected);
    					//System.out.println(doubleMajorSelected);
    					
    					//TODO Add code that actually changes the major and/or 
    					//     double major.
    					
    					LinkedList<String> lS = new LinkedList<String>();
    					lS.add(majorSelected);
    					textField_Major.setText(lS.get(0));
    					if(majorSelected.equals("Computer Science Major (CMSC)")){
    						txtField_Minor.setText("");
    						button_ChangeMinor.setVisible(false);
    					} else {
    						button_ChangeMinor.setVisible(true);
    					}
    					
    					dispose();
    				}
    			}
    		});

    		buttonPane.add(btnSave);
    		getRootPane().setDefaultButton(btnSave);

    		JButton button_cancelSelectMajor = new JButton("Cancel");
    		button_cancelSelectMajor.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent arg0) {
    				setVisible(false);
    			}
    		});
    		
    		button_cancelSelectMajor.setActionCommand("Cancel");
    		buttonPane.add(button_cancelSelectMajor);
    	}
    }

    class TermSelector extends JDialog {

    	private static final long serialVersionUID = 1L;
    	private final JPanel contentPanel = new JPanel();
    	private String[] term = {"","Spring 2010", "Summer 2010", "Fall 2010", "Winter 2011",
    			"Spring 2011", "Summer 2011", "Fall 2011", "Winter 2012", 
    			"Spring 2012", "Summer 2012", "Fall 2012", "Winter 2013", "Spring 2013", 
    			"Summer 2013", "Fall 2013", "Winter 2014", "Spring 2014", "Summer 2014", 
    			"Fall 2014", "Winter 2015", "Spring 2015", "Summer 2015", "Fall 2015", 
    			"Winter 2015"};

    	/**
    	 * Create the Term Selector dialog.
    	 */
    	public TermSelector() {
    		setResizable(false);
    		setTitle("Virtual Advisor - Term Selector");
    		setBounds(100, 100, 348, 123);
    		getContentPane().setLayout(new BorderLayout());
    		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    		getContentPane().add(contentPanel, BorderLayout.CENTER);
    		contentPanel.setLayout(null);
    		
    		JLabel label_TermSelector = new JLabel("Select Term to Add Course");
    		label_TermSelector.setFont(new Font("Tahoma", Font.BOLD, 13));
    		label_TermSelector.setBounds(10, 10, 210, 14);
    		contentPanel.add(label_TermSelector);

    		final JComboBox comboBox_TermSelector = new JComboBox(term);
    		comboBox_TermSelector.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    			}
    		});

    		comboBox_TermSelector.setBounds(10, 30, 261, 22);
    		contentPanel.add(comboBox_TermSelector);

    		JPanel buttonPane = new JPanel();
    		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		getContentPane().add(buttonPane, BorderLayout.SOUTH);

    		JButton button_AddCourseToSchedule = new JButton("Add Course to Schedule");
    		button_AddCourseToSchedule.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				String termSelected = (String)comboBox_TermSelector.getSelectedItem();
    				if(termSelected.equals("")){
    					JOptionPane.showMessageDialog(contentPanel,
    							"Please Select Term.",
    							"Warning", JOptionPane.WARNING_MESSAGE);
    				} else {
    					//System.out.println("Term Selected: " + termSelected);
    					// TODO Code to add Course to Schedule, termSelected
    					//String s = (String)comboBox.getSelectedItem();
    					//if(s != null)
    					//s = s.trim();
    					String[] ss = termSelected.split(" ");
    					Plan p = dp.getPlan(mainProfile.getCurrentPlanName(), ss[0]+ss[1]);
    					if(p == null)
    					{
    						p = new Plan(mainProfile.planNamer());
    						dp.addPlan(p);
    					}
    					
    					String[] seasonYear = termSelected.split(" ");
    					
    					//System.out.println("Season: " + seasonYear[0]);
    					//System.out.println("Year: " + seasonYear[1]);
    					
    					if(p.contains(currentCourseSelected))
    					{
    						//System.out.println("The course is already in the plan");
    						JOptionPane.showMessageDialog(contentPanel, 
    								"The course is already in the plan.","Error", 
    								JOptionPane.ERROR_MESSAGE);
    					}
    					else
    					{
    					Semester st = p.getSemester(seasonYear[0], Integer.parseInt(seasonYear[1]));
    					//System.out.println(st);
    					if(st == null)
    					{
    						st = new Semester(seasonYear[0], Integer.parseInt(seasonYear[1]));
    						p.addSemester(st);
    						String y = (String) comboBox_TermSelector.getSelectedItem();
    						comboBox.addItem(y);
    					}
    					st.addCourse(currentCourseSelected);
    					dp.addCompleteCourse(currentCourseSelected);
    					//System.out.println(s.toString());
    					}
    					dispose();
    				}
    			}
    		});

    		button_AddCourseToSchedule.setActionCommand("OK");
    		buttonPane.add(button_AddCourseToSchedule);
    		getRootPane().setDefaultButton(button_AddCourseToSchedule);

    		JButton button_CancelTermSelector = new JButton("Cancel");
    		button_CancelTermSelector.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				dispose();
    			}
    		});
    		
    		button_CancelTermSelector.setActionCommand("Cancel");
    		buttonPane.add(button_CancelTermSelector);
    	}
    }

    class CourseComments extends JDialog {
    	private static final long serialVersionUID = 1L;
    	private final JPanel contentPanel_Comments = new JPanel();
    	private Course course;

    	/**
    	 * Create the dialog.
    	 */
    	public CourseComments(Course c) {
    		course = c;
    		setResizable(false);
    		setTitle("Virtual Advisor - Comments");
    		setBounds(100, 100, 448, 223);
    		getContentPane().setLayout(null);
    		
    		final JTextArea textArea = new JTextArea();
    		textArea.setText(course.getNotes());
    		textArea.setLineWrap(true);
    		textArea.setBounds(10, 46, 422, 113);
    		getContentPane().add(textArea);
    		
    		
    		contentPanel_Comments.setBounds(441, 0, 1, 165);
    		contentPanel_Comments.setBorder(new EmptyBorder(5, 5, 5, 5));
    		getContentPane().add(contentPanel_Comments);
    		contentPanel_Comments.setLayout(null);
    		
    		JPanel buttonPanel_Comments = new JPanel();
    		buttonPanel_Comments.setBounds(0, 165, 442, 33);
    		buttonPanel_Comments.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		getContentPane().add(buttonPanel_Comments);
    			
    		JButton okButton_Comments = new JButton("Add Comments");
    		okButton_Comments.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				String text = textArea.getText();
    				//System.out.println(text);
    				//TODO Add Code to save comments, text is the omments
    				course.setNotes(text);
    				setVisible(false);
    			}
    		});
    				
    		okButton_Comments.setActionCommand("OK");
    		buttonPanel_Comments.add(okButton_Comments);
    		getRootPane().setDefaultButton(okButton_Comments);
    			
    		JButton cancelButton_Comments = new JButton("Cancel");
    		cancelButton_Comments.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent arg0) {
    				setVisible(false);
    			}
    		});
    		cancelButton_Comments.setActionCommand("Cancel");
    		buttonPanel_Comments.add(cancelButton_Comments);
    				
    		JLabel label_Comments = new JLabel("Comments:");
    		label_Comments.setBounds(10, 10, 74, 14);
    		getContentPane().add(label_Comments);
    		label_Comments.setFont(new Font("Tahoma", Font.BOLD, 13));
    		
    		JLabel label_CourseName = new JLabel(c.getDept() + " " + c.getNumber());
    		label_CourseName.setBounds(90, 10, 127, 14);
    		getContentPane().add(label_CourseName);
    		label_CourseName.setFont(new Font("Tahoma", Font.BOLD, 11));
    	}
    }

    class AddGrade extends JDialog {

    	private static final long serialVersionUID = 1L;
    	private final JPanel contentPanel = new JPanel();
    	private Course currentCourse;
    	private JTextField text_CourseName;
    	private JComboBox comboBox_Grade;

    	/**
    	 * Create the dialog.
    	 */
    	public AddGrade(Course course) {
    		currentCourse = course;
    		setResizable(false);
    		setTitle("Virtual Advisor - Add Grade");
    		setBounds(100, 100, 450, 132);
    		getContentPane().setLayout(new BorderLayout());
    		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    		getContentPane().add(contentPanel, BorderLayout.CENTER);
    		contentPanel.setLayout(null);
    		
    		JLabel label_AddGrade = new JLabel("Add Grade");
    		label_AddGrade.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		label_AddGrade.setBounds(10, 10, 164, 14);
    		contentPanel.add(label_AddGrade);
    		
    		comboBox_Grade = new JComboBox();
    		comboBox_Grade.setModel(new DefaultComboBoxModel(new Character[] {' ', 'A', 'B', 'C', 'D', 'E', 'F'}));
    		comboBox_Grade.setBounds(369, 28, 75, 22);
    		
    		contentPanel.add(comboBox_Grade);
    		
    		text_CourseName = new JTextField();
    		text_CourseName.setFont(new Font("Tahoma", Font.PLAIN, 11));
    		text_CourseName.setText(course.getDept() + " " + course.getNumber());
    		text_CourseName.setEditable(false);
    		text_CourseName.setBounds(10, 30, 347, 20);
    		contentPanel.add(text_CourseName);
    		text_CourseName.setColumns(10);
    		
    		JPanel buttonPane = new JPanel();
    		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		getContentPane().add(buttonPane, BorderLayout.SOUTH);

    		JButton button_AddGrade = new JButton("Add Grade");
    		button_AddGrade.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				Character cc = (Character) comboBox_Grade.getSelectedItem();
    				if(cc.equals(' ')){
    					JOptionPane.showMessageDialog(contentPanel,
    							"Please Select Grade.",
    							"Warning", JOptionPane.WARNING_MESSAGE);
    				} else {
    					dispose();
    					currentCourse.setGrade(cc);
    				}
    			}
    		});
    		button_AddGrade.setActionCommand("OK");
    		buttonPane.add(button_AddGrade);
    		getRootPane().setDefaultButton(button_AddGrade);

    		JButton cancelButton = new JButton("Cancel");
    		cancelButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				dispose();
    			}
    		});
    		cancelButton.setActionCommand("Cancel");
    		buttonPane.add(cancelButton);
    	}
    }
}


